<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"tallgy.gitee.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="JavaScript类型转换参考的文章 12https:&#x2F;&#x2F;www.cnblogs.com&#x2F;shikaka&#x2F;p&#x2F;10463293.htmlhttps:&#x2F;&#x2F;www.cnblogs.com&#x2F;shikaka&#x2F;p&#x2F;10473462.html    基本数据类型1string， number， Boolean， null， undefined， symbol  基本数据类型是没有方法可以调用的。 但是我们却">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaScript类型转换">
<meta property="og:url" content="http://tallgy.gitee.io/2021/10/19/JavaScript/%E9%9A%8F%E7%AC%94/JavaScript%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2/index.html">
<meta property="og:site_name" content="tallgy&#39;s blog">
<meta property="og:description" content="JavaScript类型转换参考的文章 12https:&#x2F;&#x2F;www.cnblogs.com&#x2F;shikaka&#x2F;p&#x2F;10463293.htmlhttps:&#x2F;&#x2F;www.cnblogs.com&#x2F;shikaka&#x2F;p&#x2F;10473462.html    基本数据类型1string， number， Boolean， null， undefined， symbol  基本数据类型是没有方法可以调用的。 但是我们却">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://tallgy.gitee.io/.io//image-20211019105728109.png">
<meta property="article:published_time" content="2021-10-19T02:07:37.000Z">
<meta property="article:modified_time" content="2023-06-25T08:57:29.338Z">
<meta property="article:author" content="tallgy">
<meta property="article:tag" content="JavaScript">
<meta property="article:tag" content="类型转换">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://tallgy.gitee.io/.io//image-20211019105728109.png">


<link rel="canonical" href="http://tallgy.gitee.io/2021/10/19/JavaScript/%E9%9A%8F%E7%AC%94/JavaScript%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://tallgy.gitee.io/2021/10/19/JavaScript/%E9%9A%8F%E7%AC%94/JavaScript%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2/","path":"2021/10/19/JavaScript/随笔/JavaScript类型转换/","title":"JavaScript类型转换"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>JavaScript类型转换 | tallgy's blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">tallgy's blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#JavaScript%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="nav-number">1.</span> <span class="nav-text">JavaScript类型转换</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="nav-number">1.1.</span> <span class="nav-text">基本数据类型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#valueOf-NaN"><span class="nav-number">1.2.</span> <span class="nav-text">valueOf</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%BF%99%E5%B0%B1%E6%98%AF%E6%89%A7%E8%A1%8C%E7%9A%84%E7%BB%93%E6%9E%9C%E3%80%82"><span class="nav-number">1.2.0.1.</span> <span class="nav-text">这就是执行的结果。</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%88%91%E4%BB%AC%E5%8F%AF%E4%BB%A5%E4%BB%8E%E7%BB%93%E6%9E%9C%E9%87%8C%E9%9D%A2%E7%9C%8B%E5%88%B0"><span class="nav-number">1.2.0.2.</span> <span class="nav-text">我们可以从结果里面看到</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#toString-NaN"><span class="nav-number">1.3.</span> <span class="nav-text">toString</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="nav-number">2.</span> <span class="nav-text">隐式类型转换</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AF%B9%E4%BA%8E%E4%B8%80%E4%B8%AA%E5%AF%B9%E8%B1%A1%E8%BD%AC%E4%B8%BA%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E7%9A%84%E6%96%B9%E6%B3%95"><span class="nav-number">2.0.1.</span> <span class="nav-text">对于一个对象转为基本数据类型的方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2%E3%80%82"><span class="nav-number">2.0.2.</span> <span class="nav-text">转字符串。</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BD%AC%E6%95%B0%E5%AD%97"><span class="nav-number">2.0.3.</span> <span class="nav-text">转数字</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AF%B9%E4%BA%8E%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="nav-number">2.0.3.1.</span> <span class="nav-text">对于字符串</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AF%B9%E4%BA%8E%E5%AF%B9%E8%B1%A1"><span class="nav-number">2.0.3.2.</span> <span class="nav-text">对于对象</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BD%AC%E5%B8%83%E5%B0%94%E5%80%BC"><span class="nav-number">2.0.4.</span> <span class="nav-text">转布尔值</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9F%BA%E6%9C%AC%E4%B8%8A%E6%B6%89%E5%8F%8A%E8%BF%90%E7%AE%97%E5%92%8C%E6%AF%94%E8%BE%83%E5%B0%B1%E4%BC%9A%E5%87%BA%E7%8E%B0%E9%9A%90%E5%BC%8F%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="nav-number">2.1.</span> <span class="nav-text">基本上涉及运算和比较就会出现隐式类型转换</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8A%A0%E5%8F%B7"><span class="nav-number">2.1.1.</span> <span class="nav-text">加号 +</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%87%8F%E5%8F%B7-%E4%B9%98%E5%8F%B7-%E9%99%A4%E5%8F%B7"><span class="nav-number">2.1.2.</span> <span class="nav-text">减号 - 乘号 * 除号 &#x2F;</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%AF%94%E8%BE%83%E8%BF%90%E7%AE%97"><span class="nav-number">2.1.3.</span> <span class="nav-text">比较运算</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%92%8C"><span class="nav-number">2.1.3.1.</span> <span class="nav-text">&#x3D;&#x3D; 和 &#x3D;&#x3D;&#x3D;</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#null%E5%92%8Cundefined%E7%9A%84%E6%AF%94%E8%BE%83"><span class="nav-number">2.1.3.2.</span> <span class="nav-text">null和undefined的比较</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#NaN-%E4%B8%8D%E4%B8%8E%E4%BB%BB%E4%BD%95%E5%80%BC%E7%9B%B8%E7%AD%89%EF%BC%8C%E5%8C%85%E6%8B%AC%E8%87%AA%E5%B7%B1"><span class="nav-number">2.1.3.3.</span> <span class="nav-text">NaN 不与任何值相等，包括自己</span></a></li></ol></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">tallgy</p>
  <div class="site-description" itemprop="description">只是一个知识的搬运工</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">107</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">74</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://tallgy.gitee.io/2021/10/19/JavaScript/%E9%9A%8F%E7%AC%94/JavaScript%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tallgy">
      <meta itemprop="description" content="只是一个知识的搬运工">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tallgy's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          JavaScript类型转换
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-10-19 10:07:37" itemprop="dateCreated datePublished" datetime="2021-10-19T10:07:37+08:00">2021-10-19</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2023-06-25 16:57:29" itemprop="dateModified" datetime="2023-06-25T16:57:29+08:00">2023-06-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/JavaScript/" itemprop="url" rel="index"><span itemprop="name">JavaScript</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/JavaScript/Global-Objects/" itemprop="url" rel="index"><span itemprop="name">Global_Objects</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="JavaScript类型转换"><a href="#JavaScript类型转换" class="headerlink" title="JavaScript类型转换"></a>JavaScript类型转换</h1><p>参考的文章</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">https://www.cnblogs.com/shikaka/p/10463293.html</span><br><span class="line">https://www.cnblogs.com/shikaka/p/10473462.html</span><br></pre></td></tr></table></figure>



<h2 id="基本数据类型"><a href="#基本数据类型" class="headerlink" title="基本数据类型"></a>基本数据类型</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">string， number， Boolean， null， undefined， symbol</span><br></pre></td></tr></table></figure>

<p>基本数据类型是没有方法可以调用的。</p>
<p>但是我们却发现，</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#x27;aaa&#x27;.indexOf(&#x27;a&#x27;);</span><br></pre></td></tr></table></figure>

<p>这个明明是基本数据类型，但是却可以调用方法，这个是为什么呢。</p>
<p>这个是因为<code>JavaScript</code>在底层进行了一个操作，当发现调用方法的是一个基本数据类型的话，会根据这个类型来创建一个对应的引用数据类型对象。然后再调用这个方法，调用之后将返回值进行返回并将这个对象进行删除。</p>
<p>在 <code>JavaScript</code> 中，类型转换主要的两个方法是</p>
<p><code>valueOf</code> ， <code>toString</code></p>
<p>我们下面分别对这两个进行一个判断使用操作。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">let a = 1,</span><br><span class="line">    b = &#x27;a&#x27;,</span><br><span class="line">    c = true,</span><br><span class="line">    d = undefined,</span><br><span class="line">    e = null,</span><br><span class="line">    f = [1, 2, 3],</span><br><span class="line">    g = [&#x27;1&#x27;, &#x27;3&#x27;],</span><br><span class="line">    h = new Number(3),</span><br><span class="line">    i = new String(&#x27;12&#x27;),</span><br><span class="line">    j = new Boolean(false),</span><br><span class="line">    k = new Object(&#123; a: 3&#125; ),</span><br><span class="line">    l = &#123; b: 4 &#125;,</span><br><span class="line">    m = function () &#123;&#125;,</span><br><span class="line">    n = new Date(),</span><br><span class="line">    o = new RegExp(&#x27;sss&#x27;),</span><br><span class="line">    p = /sss/,</span><br><span class="line">    q = `sdcard`,</span><br><span class="line">    r = new Array([1,2,3]);</span><br></pre></td></tr></table></figure>



<h2 id="valueOf-NaN"><a href="#valueOf-NaN" class="headerlink" title="valueOf"></a>valueOf</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">console.log(a.valueOf());</span><br><span class="line">console.log(b.valueOf());</span><br><span class="line">console.log(c.valueOf());</span><br><span class="line">// console.log(d.valueOf());</span><br><span class="line">// console.log(e.valueOf());</span><br><span class="line">console.log(f.valueOf());</span><br><span class="line">console.log(h.valueOf());</span><br><span class="line">console.log(i.valueOf());</span><br><span class="line">console.log(j.valueOf());</span><br><span class="line">console.log(k.valueOf());</span><br><span class="line">console.log(n.valueOf());</span><br><span class="line">console.log(o.valueOf());</span><br></pre></td></tr></table></figure>

<h4 id="这就是执行的结果。"><a href="#这就是执行的结果。" class="headerlink" title="这就是执行的结果。"></a>这就是执行的结果。</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">1</span><br><span class="line">a</span><br><span class="line">true</span><br><span class="line">[ 1, 2, 3 ]</span><br><span class="line">3</span><br><span class="line">12</span><br><span class="line">false</span><br><span class="line">&#123; a: 3 &#125;</span><br><span class="line">1634610286344</span><br><span class="line">/sss/</span><br></pre></td></tr></table></figure>

<h4 id="我们可以从结果里面看到"><a href="#我们可以从结果里面看到" class="headerlink" title="我们可以从结果里面看到"></a>我们可以从结果里面看到</h4><table>
<thead>
<tr>
<th>类型</th>
<th>结果</th>
</tr>
</thead>
<tbody><tr>
<td>null，undefined</td>
<td>没有valueOf方法，会报错</td>
</tr>
<tr>
<td>基本数据类型，包括Symbol类型</td>
<td>valueOf方法为其本身，可以使用 === 来判断，Symbol为true。</td>
</tr>
<tr>
<td>对象，数组，方法，正则</td>
<td>也为其本身</td>
</tr>
<tr>
<td>使用new方法创建的基本数据类型，new number(1)</td>
<td>为其值，与原来不等。</td>
</tr>
<tr>
<td>date类型</td>
<td>当前时间距 1970.01.01 午夜的毫秒数</td>
</tr>
</tbody></table>
<p>所以我们可以看到，</p>
<p>基本除了对象，其他的类型使用valueOf方法都可以转换成基本数据类型。</p>
<p>对于使用new方法创建的基本数据类型的引用数据类型，也会被转换成基本数据类型。</p>
<p>这里的原因，是因为 <code>number </code>对象对 <code>valueOf</code> 进行了一次重写，所以在调用的时候就会调用自己所重写的方法，于是返回的结果会不同。其他也同理。</p>
<p>但是<code>JavaScript</code>内部是如何进行操作，取出值的，我还是不了解。</p>
<h2 id="toString-NaN"><a href="#toString-NaN" class="headerlink" title="toString"></a>toString</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">console.log(a.toString());</span><br><span class="line">console.log(b.toString());</span><br><span class="line">console.log(c.toString());</span><br><span class="line">// console.log(d.toString());</span><br><span class="line">// console.log(e.toString());</span><br><span class="line">console.log(f.toString());</span><br><span class="line">console.log(h.toString());</span><br><span class="line">console.log(i.toString());</span><br><span class="line">console.log(j.toString());</span><br><span class="line">console.log(k.toString());</span><br><span class="line">console.log(n.toString());</span><br><span class="line">console.log(o.toString());</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">1</span><br><span class="line">a</span><br><span class="line">true</span><br><span class="line">1,2,3</span><br><span class="line">3</span><br><span class="line">12</span><br><span class="line">false</span><br><span class="line">[object Object]</span><br><span class="line">Tue Oct 19 2021 10:50:07 GMT+0800 (中国标准时间)</span><br><span class="line">/sss/</span><br></pre></td></tr></table></figure>



<table>
<thead>
<tr>
<th>类型</th>
<th>结果</th>
</tr>
</thead>
<tbody><tr>
<td>null， undefined</td>
<td>没有toString方法</td>
</tr>
<tr>
<td>基本数据类型</td>
<td>直接转为字符串</td>
</tr>
<tr>
<td>普通对象</td>
<td>直接转为字符串 [object Object]</td>
</tr>
<tr>
<td>日期，正则对象</td>
<td>转为对应值的字符串</td>
</tr>
</tbody></table>
<h1 id="隐式类型转换"><a href="#隐式类型转换" class="headerlink" title="隐式类型转换"></a>隐式类型转换</h1><p>我这里主要讲解一下隐式类型转换</p>
<h3 id="对于一个对象转为基本数据类型的方法"><a href="#对于一个对象转为基本数据类型的方法" class="headerlink" title="对于一个对象转为基本数据类型的方法"></a>对于一个对象转为基本数据类型的方法</h3><p>对于一个不是基本数据类型的。会先通过 <code>valueOf</code> 转换，看是否能转为基本数据类型</p>
<p>如果还不可以，然后就会使用 <code>toString </code>方法转为字符串。然后再来进行判断。</p>
<p>对于 <code>Date</code> 对象的话，使用顺序会反过来</p>
<h3 id="转字符串。"><a href="#转字符串。" class="headerlink" title="转字符串。"></a>转字符串。</h3><p>就直接toString方法就行了。</p>
<h3 id="转数字"><a href="#转数字" class="headerlink" title="转数字"></a>转数字</h3><p>就类似于 <code>Number(undefined); </code> 的情况</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">null， false		-&gt; 0</span><br><span class="line">undefined		-&gt; NaN</span><br></pre></td></tr></table></figure>

<h4 id="对于字符串"><a href="#对于字符串" class="headerlink" title="对于字符串"></a>对于字符串</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">字符串含有字符的。没有特殊含义的都为 NaN</span><br><span class="line"></span><br><span class="line">&#x27;1.111111111111111e+50&#x27; 这个就会转为数字，还有进制表示和小数点会转，其他带有字符会被转成 NaN</span><br></pre></td></tr></table></figure>

<h4 id="对于对象"><a href="#对于对象" class="headerlink" title="对于对象"></a>对于对象</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">先使用前面我说过的规则转为基本类型，然后再进行转换。</span><br></pre></td></tr></table></figure>



<h3 id="转布尔值"><a href="#转布尔值" class="headerlink" title="转布尔值"></a>转布尔值</h3><p>几个特殊值为 <code>false</code></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">NaN, undefined， null， 0， +0， -0， &#x27;&#x27;</span><br></pre></td></tr></table></figure>

<p>其他为true</p>
<h2 id="基本上涉及运算和比较就会出现隐式类型转换"><a href="#基本上涉及运算和比较就会出现隐式类型转换" class="headerlink" title="基本上涉及运算和比较就会出现隐式类型转换"></a>基本上涉及运算和比较就会出现隐式类型转换</h2><h3 id="加号"><a href="#加号" class="headerlink" title="加号 +"></a>加号 +</h3><p>存在字符串则转为字符串的拼接操作</p>
<p>非字符串就会转数字操作。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">console.log(1+2+&#x27;&#x27;)</span><br><span class="line">1+2 先为3，</span><br><span class="line">然后3+&#x27;&#x27; 为3</span><br></pre></td></tr></table></figure>

<p>这个就涉及到了上面的转换规则</p>
<p>这里有个小问题</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">let x = Symbol(1);</span><br><span class="line">console.log(x + &#x27;1&#x27;)</span><br><span class="line">这样会抛出异常，虽然Symbol可以使用toString，</span><br><span class="line">但是不能进行隐式转换。</span><br></pre></td></tr></table></figure>



<h3 id="减号-乘号-除号"><a href="#减号-乘号-除号" class="headerlink" title="减号 - 乘号 * 除号 /"></a>减号 - 乘号 * 除号 /</h3><p>会转为数字进行运算。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">特殊的，对于 NaN运算得NaN</span><br><span class="line">对于 0/0 等于 NaN</span><br><span class="line">对于 1/0 等于 Infinity</span><br></pre></td></tr></table></figure>



<p>|| 和 &amp;&amp; 这里这个返回值不是<code>true</code>和<code>false</code>而是返回两边操作数的执行结果。</p>
<h3 id="比较运算"><a href="#比较运算" class="headerlink" title="比较运算"></a>比较运算</h3><p><strong>转换规则</strong></p>
<img src="/.io//image-20211019105728109.png" alt="image-20211019105728109" style="zoom:67%;">



<h4 id="和"><a href="#和" class="headerlink" title="== 和 ==="></a>== 和 ===</h4><p>非严格相等等号 可以允许强制类型转换，</p>
<p>而严格相等等号 不允许强制类型的转换。</p>
<p>其他的就如上，使用上面的转换规则进行转换。</p>
<p>这里有个小问题点</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">if ([]) &#123;</span><br><span class="line">  console.log(1)</span><br><span class="line">&#125; else &#123;</span><br><span class="line">  console.log(2)</span><br><span class="line">&#125;</span><br><span class="line">此时会输出为1，所以说明了[]在if判断里面为真，但是 [] == true 为false，</span><br><span class="line">这里可以看上面 转布尔值 那个地方，说明了，布尔在进行判断时 除了 undefined，null，NaN，0， &#x27;&#x27;，其他都为true</span><br><span class="line">因此也可以说明，if判断是使用的 Boolean 来进行的判断为真还是为假。而不是与true进行的比较。</span><br></pre></td></tr></table></figure>



<h4 id="null和undefined的比较"><a href="#null和undefined的比较" class="headerlink" title="null和undefined的比较"></a>null和undefined的比较</h4><p>null和undefined进行比较时</p>
<p>​    使用非严格等号为 true</p>
<p>​    使用严格等号为 false</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">null == undefined</span><br></pre></td></tr></table></figure>



<h4 id="NaN-不与任何值相等，包括自己"><a href="#NaN-不与任何值相等，包括自己" class="headerlink" title="NaN 不与任何值相等，包括自己"></a>NaN 不与任何值相等，包括自己</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">NaN == NaN	false</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">并且 NaN &gt; 0 和 NaN &lt;= 0 都为 false</span><br></pre></td></tr></table></figure>






    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/JavaScript/" rel="tag"># JavaScript</a>
              <a href="/tags/%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2/" rel="tag"># 类型转换</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/10/18/JavaScript/%E6%96%B9%E6%B3%95/call-apply-bind%E5%AE%9E%E7%8E%B0/" rel="prev" title="call,apply,bind实现">
                  <i class="fa fa-chevron-left"></i> call,apply,bind实现
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/10/19/%E9%9A%8F%E7%AC%94/Git%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/" rel="next" title="Git常用命令">
                  Git常用命令 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">tallgy</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
